package breeze.pixel.weather.apps_util.utils.appsettings.settingsview;

import android.annotation.SuppressLint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;

import breeze.pixel.weather.BaseView;
import breeze.pixel.weather.R;
import breeze.pixel.weather.apps_util.utils.AppToolUtil;
import breeze.pixel.weather.apps_util.utils.MDialog;
import breeze.pixel.weather.apps_util.views.mtextview.MTextView;

public class SetTextStyleView extends BaseView {

    MTextView mtextsize_city_name;
    MTextView mtextsize_city_id;
    LinearLayout mtextsize1_more_info_line;
    MTextView mtextsize_up_city_name;
    MTextView mtextsize_xingzheng_city_name;
    MTextView mtextsize_country_city_name;
    MTextView mtextsize_areatime_city_name;
    LinearLayout mactivitytextsize1LinearLayout1;
    ImageView mtextsize2_weather_icon;
    MTextView mtextsize2_weatherName;
    MTextView mtextsize2_tem_text;
    MTextView mtextsize2_wet, text_size;
    MTextView mtextsize2_wind_direct_n;
    SeekBar mtextsize_seek;
    private int textsize,textFont;
    private String textFontPath = "";
    private RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_textsize);
        init();
        initData();
    }

    @Override
    public void init() {
        mtextsize_city_name = findViewById(R.id.textsize_city_name);
        mtextsize_city_name.setTextSize(30);
        mtextsize_city_id = findViewById(R.id.textsize_city_id);
        mtextsize1_more_info_line = findViewById(R.id.textsize1_more_info_line);
        mtextsize_up_city_name = findViewById(R.id.textsize_up_city_name);
        mtextsize_xingzheng_city_name = findViewById(R.id.textsize_xingzheng_city_name);
        mtextsize_country_city_name = findViewById(R.id.textsize_country_city_name);
        mtextsize_areatime_city_name = findViewById(R.id.textsize_areatime_city_name);
        mactivitytextsize1LinearLayout1 = findViewById(R.id.activitytextsize1LinearLayout1);
        mtextsize2_weather_icon = findViewById(R.id.textsize2_weather_icon);
        mtextsize2_weatherName = findViewById(R.id.textsize2_weatherName);
        mtextsize2_tem_text = findViewById(R.id.textsize2_tem_text);
        mtextsize2_tem_text.setTextSize(25);
        mtextsize2_wet = findViewById(R.id.textsize2_wet);
        mtextsize2_wind_direct_n = findViewById(R.id.textsize2_wind_direct_n);
        mtextsize_seek = findViewById(R.id.textsize_seek);
        text_size = findViewById(R.id.textsize_size);
        radioGroup = find(R.id.set_textstyle_group);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void initData() {
        textsize = sets.getTextSize();
        if (textsize < 10) {
            textsize = 10;
        } else if (textsize > 20) {
            textsize = 20;
        }
        if (textsize == 15) {
            text_size.setText(String.format("字体大小:%s[推荐]", textsize));
        } else {
            text_size.setText(String.format("字体大小:%s", textsize));
        }
        mtextsize_seek.setMax(10);
        mtextsize_seek.setProgress(textsize - 10);
        mtextsize_seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar p1, int p2, boolean p3) {
                if (p2 == 5) {
                    text_size.setText(String.format("字体大小:%s[推荐]", (p2 + 10)));
                } else {
                    text_size.setText(String.format("字体大小:%s", (p2 + 10)));
                }
                textsize = p2 + 10;
                update();
            }

            @Override
            public void onStartTrackingTouch(SeekBar p1) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar p1) {
            }

        });
        ((RadioButton)radioGroup.getChildAt(sets.getTextFont())).setChecked(true);
        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.set_textstyle_system_font:
                    textFont = 0;
                    setTextFont(0);
                    break;
                case R.id.set_textstyle_pixel1_font:
                default:
                    textFont = 1;
                    setTextFont(1);
                    break;
                case R.id.set_textstyle_pixel2_font:
                    textFont = 2;
                    setTextFont(2);
                    break;
            }
        });
    }

    private void update() {
        mtextsize2_wet.setTextSize(textsize);
        mtextsize_city_id.setTextSize(textsize);
        mtextsize2_wind_direct_n.setTextSize(textsize);
        mtextsize_country_city_name.setTextSize(textsize);
        mtextsize_xingzheng_city_name.setTextSize(textsize);
        mtextsize_up_city_name.setTextSize(textsize);
        mtextsize2_weatherName.setTextSize(textsize);
        mtextsize_areatime_city_name.setTextSize(textsize);
    }

    public void save(View v) {
        sets.setTextSize(textsize);
        sets.setTextFont(textFont);
        MDialog dialog = new MDialog(this);
        dialog.setTitle("提示");
        dialog.setMessage("修改字体或文字大小后需要重启APP噢，是否现在重启？");
        dialog.setPositiveButton("现在重启", () -> AppToolUtil.reStartApp(SetTextStyleView.this));
        dialog.setNegativeBotton("稍后", null);
        dialog.show();
    }

    private void setTypeface(Typeface font){
        mtextsize2_wet.setTypeface(font);
        mtextsize_city_id.setTypeface(font);
        mtextsize2_wind_direct_n.setTypeface(font);
        mtextsize_country_city_name.setTypeface(font);
        mtextsize_xingzheng_city_name.setTypeface(font);
        mtextsize2_tem_text.setTypeface(font);
        mtextsize_up_city_name.setTypeface(font);
        mtextsize2_weatherName.setTypeface(font);
        mtextsize_areatime_city_name.setTypeface(font);
    }

    private void setTextFont(int font){
        /*
         * 0：无字体
         * 1：原本字体
         * 2：粗大字体
         * 3：自定义字体
         */
        if(font==-1){
            font = sets.getTextFont();
        }
        switch (font){
            case 0:
                setTypeface(null);
                break;
            case 1:
            default:
                setTypeface(createTypeFace("fonts/Pixel.ttf"));
                break;
            case 2:
                setTypeface(createTypeFace("fonts/Pixel2.ttf"));
                break;
            case 3:
                setTypeface(createTypeFaceByFile(textFontPath));
                break;
        }
    }
    private Typeface createTypeFace(String path){
        return Typeface.createFromAsset(getAssets(), path);
    }

    private Typeface createTypeFaceByFile(String path){
        return Typeface.createFromFile(path);
    }


}